package eventplanner.rest;

import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.response.ResponseBody;

import static com.jayway.restassured.RestAssured.expect;

public class RestUtil {

	public static final String BASE_URL = "/eventplanner/rest";
	public static final String COOKIE_NAME = "JSESSIONID";


	public static String getLoginCookie() {
		Response post =
				expect()
						.statusCode(200)
						.cookie("JSESSIONID")
						.contentType(ContentType.JSON)
						.given()
						.parameter("username", "lalala")
						.post(BASE_URL + "/user/signin");
		return post.getCookie("JSESSIONID");
	}

	public static String getTokenString(String cookie) {
		Response response =
				expect()
						.contentType(ContentType.JSON)
						.statusCode(200)
						.given()
						.cookie("JSESSIONID", cookie)
						.get(BASE_URL + "/token/tokens");

		String s = response.getBody().asString();
		System.out.println("Tokens: " + s);
		s = s.substring(2, 38);

//		System.out.println(s);
		return s;
	}

	public static long getTokenId(String tokenString) {
		Response response =
				expect()
						.statusCode(200)
						.contentType(ContentType.JSON)
						.get(BASE_URL + "/token/" + tokenString);
		ResponseBody body = response.getBody();
		return body.jsonPath().getLong("[0][0]");
	}
}
